package com.trex.modules.tbl.kclb.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trex.modules.tbl.lineform.entity.TblXdZxxdctzd;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.kclb.entity.TblZdKclb;
import com.trex.modules.tbl.kclb.entity.TblZdKcyy;
import com.trex.modules.tbl.kclb.service.TblZdKclbService;
import com.trex.modules.tbl.kclb.service.TblZdKcyyService;

/**
 * 扣车原因Controller
 * @author jy
 * @version 2022-12-06
 */
@Controller
@RequestMapping(value = "/tbl/kclb/tblZdKcyy")
public class TblZdKcyyController extends BaseController {

	@Autowired
	private TblZdKcyyService tblZdKcyyService;
	
	@Autowired
	private TblZdKclbService tblZdKclbService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TblZdKcyy get(String recId, boolean isNewRecord) {
		return tblZdKcyyService.get(recId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:view")
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(value = "userid", required = false) String userid,
					   @RequestParam(value = "postid", required = false) String postid,TblZdKcyy tblZdKcyy, Model model) {

		if (userid != null) {
			// 将 userid 添加到模型中或者在逻辑中使用
			tblZdKcyy.setUserid(userid);
		}
		if (postid != null) {
			// 将 postid 添加到模型中或者在逻辑中使用
			tblZdKcyy.setPostid(postid);
		}
		model.addAttribute("tblZdKcyy", tblZdKcyy);
		List<TblZdKclb> kclbList = tblZdKclbService.findList(new TblZdKclb());

		kclbList.add(0, new TblZdKclb());
		model.addAttribute("kclbList", kclbList);

		return "modules/tbl/kclb/tblZdKcyyList";
	}
	
	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TblZdKcyy> listData(TblZdKcyy tblZdKcyy, HttpServletRequest request, HttpServletResponse response) {
		tblZdKcyy.setPage(new Page<>(request, response));
		Page<TblZdKcyy> page = tblZdKcyyService.findPage(tblZdKcyy);
		return page;
	}

	/**
	 * 通过类别查询扣车原因数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:view")
	@RequestMapping(value = "listData2")
	@ResponseBody
	public List<TblZdKcyy> listData2(String kclb, HttpServletRequest request, HttpServletResponse response) {
		TblZdKcyy tblZdKcyy = new TblZdKcyy();
		tblZdKcyy.setKclbCode(kclb);
		tblZdKcyy.setDeleFlag("0");
		List<TblZdKcyy> listYY = tblZdKcyyService.findList2(tblZdKcyy);
		return listYY;
	}
	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:view")
	@RequestMapping(value = "form")
	public String form(TblZdKcyy tblZdKcyy, Model model) {
		model.addAttribute("tblZdKcyy", tblZdKcyy);
		
		List<TblZdKclb> kclbList = tblZdKclbService.findList(new TblZdKclb());
		kclbList.add(0, new TblZdKclb());
		List<TblXdZxxdctzd> rlcList = tblZdKcyyService.findrepairLine(new TblXdZxxdctzd());
		rlcList.add(0, new TblXdZxxdctzd());
		model.addAttribute("kclbList", kclbList);
		model.addAttribute("rlcList", rlcList);
		return "modules/tbl/kclb/tblZdKcyyForm";
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TblZdKcyy tblZdKcyy, HttpServletRequest request) {
		String I_IP_ADDR = request.getRemoteHost(); // 取IP
		String I_USERCODE = UserUtils.getUser().getUserCode(); // 取用户id
		String I_USERNAME = UserUtils.getUser().getUserName(); //用户名
		
		tblZdKcyy.setIp(I_IP_ADDR);
		tblZdKcyy.setUserCode(I_USERCODE);
		tblZdKcyy.setUserName(I_USERNAME);
		tblZdKcyy.setUpdTime(new Date());
		tblZdKcyy.setDeleFlag("0");
		
		TblZdKcyy tblZdKcyy_new = new TblZdKcyy();
		tblZdKcyy_new.setFaultCode(tblZdKcyy.getFaultCode());
		
		if(tblZdKcyy.getIsNewRecord()) {
			//新增 判断记录是否已存在
			if(tblZdKcyyService.findCount(tblZdKcyy_new) > 0) {
				return renderResult(Global.FALSE, text("扣车原因编码已存在，无法新增！"));
			};
		}else {
			//修改
			List<TblZdKcyy> listU = tblZdKcyyService.findList(tblZdKcyy_new);

			for(int a=0;a<listU.size();a++) {
				TblZdKcyy tblZdKcyy_new2 = listU.get(a);
				if(tblZdKcyy_new2.getRecId().equals(tblZdKcyy.getRecId())) {
					continue;
				}else {
					return renderResult(Global.FALSE, text("扣车原因编码已存在，修改失败！"));
				}
			}
		}
		tblZdKcyyService.save(tblZdKcyy);
		return renderResult(Global.TRUE, text("保存扣车原因成功！"));
	}
	
	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKcyy:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TblZdKcyy tblZdKcyy) {
		tblZdKcyyService.delete(tblZdKcyy);
		return renderResult(Global.TRUE, text("删除扣车原因成功！"));
	}
	
	
	/**
	 * 选择扣车原因列表
	 */
	@RequestMapping(value = {"listSelect", ""})
	public String listSelect(TblZdKcyy tblZdKcyy, Model model) {
		model.addAttribute("tblZdKcyy", tblZdKcyy);
		
		List<TblZdKclb> kclbList = tblZdKclbService.findList(new TblZdKclb());
		kclbList.add(0, new TblZdKclb());
		model.addAttribute("kclbList", kclbList);
		return "modules/tbl/kclb/tblZdKcyySelect";
	}
	
	/**
	 * 选择扣车原因列表数据
	 */
	@RequestMapping(value = "listDataSelect")
	@ResponseBody
	public List<TblZdKcyy> listDataSelect(String kclbCode, HttpServletRequest request, HttpServletResponse response) {
		TblZdKcyy tblZdKcyy = new TblZdKcyy();
		tblZdKcyy.setKclbCode(kclbCode);
		if(kclbCode.isEmpty()) {
			return tblZdKcyyService.findList(tblZdKcyy);
		}
		
		return tblZdKcyyService.findList2(tblZdKcyy);
	}
}